package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.apkextract.lib.model.AeUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AppAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.databinding.ActivityApkManageBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class ApkManageActivity extends BaseAc<ActivityApkManageBinding> {

    private AppAdapter appAdapter;
    private boolean isEdit = false;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_apk_manage;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            isEdit = false;
            appAdapter.setEdit(isEdit);
            loadApks();
            mDataBinding.ivApkManageEdit.setVisibility(View.VISIBLE);
            mDataBinding.llApkManageBottom.setVisibility(View.GONE);
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivApkManageBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.rvApkList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        appAdapter = new AppAdapter();
        appAdapter.setEdit(isEdit);
        mDataBinding.rvApkList.setAdapter(appAdapter);
        appAdapter.setOnItemClickListener(this);

        mDataBinding.ivApkManageEdit.setOnClickListener(this);
//        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivApkManageEdit://管理
                isEdit = true;
                appAdapter.setEdit(isEdit);
                appAdapter.notifyDataSetChanged();
                mDataBinding.ivApkManageEdit.setVisibility(View.GONE);
                mDataBinding.llApkManageBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.tvDelete://删除
                AppUtils.uninstallApp(appAdapter.getItem(oldPosition).pkgName);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            appAdapter.getItem(oldPosition).setSelected(false);
            appAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            appAdapter.getItem(position).setSelected(true);
            appAdapter.notifyItemChanged(position);
        } else {
            lookAppDetail(appAdapter.getItem(position).pkgName);
        }
    }

    @Override
    protected void initData() {
        loadApks();
    }

    private void loadApks() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<String>>() {
            @Override
            public void doBackground(ObservableEmitter<List<String>> emitter) {
                emitter.onNext(AeUtil.getNoSystemInstalledPackageNames());
            }

            @Override
            public void accept(List<String> packageNames) {
                if (!CollectionUtils.isEmpty(packageNames)) {
                    mDataBinding.rvApkList.setVisibility(View.VISIBLE);
                    mDataBinding.ivApkManageNoData.setVisibility(View.GONE);
                    List<ApkBean> apkBeans = new ArrayList<>();
                    ApkBean bean;
                    for (String packageName : packageNames) {
                        bean = new ApkBean();
                        bean.pkgName = packageName;
                        apkBeans.add(bean);
                    }
                    appAdapter.setList(apkBeans);
                } else {
                    mDataBinding.rvApkList.setVisibility(View.GONE);
                    mDataBinding.ivApkManageNoData.setVisibility(View.VISIBLE);
                }
                dismissDialog(500L);
            }
        });
    }

    private void lookAppDetail(String packageName) {
        Intent intent = IntentUtils.getLaunchAppDetailsSettingsIntent(packageName);
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}